package com.elontian.db.data.impl;

import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.elontian.db.data.DatabaseHelper;
import com.elontian.db.data.IURLDataDao;
import com.elontian.db.data.modle.URLData;
import com.elontian.util.CheckUtil;

public class URLDataDaoImpl implements IURLDataDao {
	private String TABLE_NAME = "URL_DATA";
	private DatabaseHelper helper;
	private SQLiteDatabase db;
	private long DIETIME = 1000 * 60 * 60 * 6;

	public URLDataDaoImpl(Context context) {
		// TODO Auto-generated constructor stub
		helper = new DatabaseHelper(context);
	}

	@Override
	public int save(URLData data) {
		// TODO Auto-generated method stub
		int result = 0;
		try {
			if (null != this.getByUrl(data.getUrl())) {
				result = this.update(data);
			} else {
				result = this.insert(data);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return result;
	}

	public int insert(URLData data) {
		long result = 0;
		// TODO Auto-generated method stub
		try {
			db = helper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put("URL", data.getUrl());
			values.put("Data", data.getData().toString());
			values.put("time", data.getTime().toString());
			result = db.insert(TABLE_NAME, null, values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (null != db) {
				db.close();
			}
		}

		return (int) result;
	}

	public int update(URLData data) {
		int result = 0;
		try {
			db = helper.getReadableDatabase();
			ContentValues values = new ContentValues();
			values.put("URL", data.getUrl());
			values.put("Data", data.getData());
			values.put("time", data.getTime().toString());
			result = db.update(TABLE_NAME, values, "URL = ?",
					new String[] { data.getUrl() });
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (null != db) {
				db.close();
			}
		}

		return result;
	}

	@Override
	public URLData getByUrl(String url) {
		// TODO Auto-generated method stub
		URLData data = null;
		Cursor cursor = null;
		try {
			String sql = "select url,data,time from " + TABLE_NAME
					+ " where url = ?";

			db = helper.getReadableDatabase();
			// db.execSQL(sql, new Object[] { url, page } );
			cursor = db.rawQuery(sql, new String[] { url });
			while (cursor.moveToNext()) {
				data = new URLData();
				data.setUrl(cursor.getString(0));
				data.setData(cursor.getString(1));
				data.setTime(cursor.getLong(2));
				if (!CheckUtil.checkDieData(data)) {
					data = null;
				}
			}

		} catch (Exception e) {
		} finally {
			if (null != cursor) {
				cursor.close();
			}
			if (null != db) {
				db.close();
			}

		}

		return data;
	}

	@Override
	public int delete(String optionid) {
		// TODO Auto-generated method stub
		int result = 0;
		try {
			String sql = "DELETE FROM " + TABLE_NAME + " WHERE URL_OPTIONID=?";
			db = helper.getWritableDatabase();
			db.execSQL(sql, new Object[] { optionid });
			result = db.delete(TABLE_NAME, "URL_OPTIONID=?",
					new String[] { optionid });
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (null != db) {
				db.close();
			}
		}
		return result;
	}

	@Override
	public int deleteDieData() {
		// TODO Auto-generated method stub
		int result = 0;
		try {
			long nowTime = new Date().getTime() - DIETIME;
			// String sql = "DELETE FROM " + TABLE_NAME + " WHERE TIME < ?";
			db = helper.getWritableDatabase();
			// db.execSQL(sql, new Object[]{nowTime});
			result = db.delete(TABLE_NAME, "TIME < ?", new String[] { ""
					+ nowTime });
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (null != db) {
				db.close();
			}
		}
		return result;
	}

}
